package com.weifei.core.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

public class WFJdkDynamicAopProxy implements WFAopProxy, InvocationHandler {

    private WFAdvisedSupport config;

    public WFJdkDynamicAopProxy(WFAdvisedSupport config) {
        this.config = config;
    }

    @Override
    public Object getProxy() {
        return this.getProxy(this.config.getTargetClass().getClassLoader());
    }

    @Override
    public Object getProxy(ClassLoader classLoader) {
        return Proxy.newProxyInstance(classLoader, this.config.getTargetClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 将每个PointJoint,也就是被代理的业务方法，封装成一个拦截器，组合成一个拦截器链
        List<Object> interceptorsAndDynamicMethodMatchers =
                config.getInterceptorsAndDynamicInterceptionAdivice(method, config.getTargetClass());
        // 交给拦截器链MethodInvocation的proceed方法执行
        WFMethodInvocation invocation = new WFMethodInvocation(proxy, this.config.getTarget(),
                method, args, this.config.getTargetClass(), interceptorsAndDynamicMethodMatchers);
        return null;
    }
}
